import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:math' as Math;
import 'dart:io';

import 'package:url_launcher/url_launcher.dart';

class Utils {

  /// 获取本地图片路径
  static String getImgPath(String name, {String format: 'png'}) {
    return 'assets/images/$name.$format';
  }

  /// 展示底部提示Bar
  static void showSnackBar(BuildContext context, String msg) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(msg),
        duration: Duration(seconds: 2),
      ),
    );
  }

  static Widget getSusItem(BuildContext context, String tag,
      {double susHeight = 40}) {
    if (tag == '★') {
      tag = '★ 热门城市';
    }
    return Container(
      height: susHeight,
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(left: 16.0),
      color: Color(0xFFF3F4F5),
      alignment: Alignment.centerLeft,
      child: Text(
        '$tag',
        softWrap: false,
        style: TextStyle(
          fontSize: 14.0,
          color: Color(0xFF666666),
        ),
      ),
    );
  }

  /// 加载本地Asset图片
  static Widget getAssetImgWithPath(String name,
      {String format: 'png'}) {
    return Image.asset('assets/images/$name.$format',
        fit: BoxFit.cover);
  }

  /// 加载本地Asset图片
  static Image getDefaultImage() {
    return Image.asset('assets/images/music_2.jpg',
        fit: BoxFit.cover);
  }

  /// 获取渐变参数
  //
  /// [colors] 渐变颜色
  /// [deg] 渐变角度
  static LinearGradient getLinearGradientParams(
      {required List<Color> colors, required double deg, required List<double> stops}) {
     Map<String, double> alignment = parseAngleToAlignment(deg);

    return LinearGradient(
        colors: colors,
        stops: stops,
        begin: Alignment(alignment['beginX']!, alignment['beginY']!),
        end: Alignment(alignment['endX']!, alignment['endY']!));
  }

  /// 把角度转为Alignment组件可用参数, 一般用于渐变
  //
  /// [start_x] 为start参数的Alignment.x
  /// [start_y] 为start参数的Alignment.y
  /// 以此类推
  static Map<String, double> parseAngleToAlignment(double deg) {
    deg = 180 - deg;

    double rad = Math.pi / 180 * deg;
    double endrad = Math.pi / 180 * (180 + deg);

    Map<String, double> result = {};

    result['beginX'] = Math.cos(rad) * Math.sqrt(2);
    result['beginY'] = -Math.sin(rad) * Math.sqrt(2);
    result['endX'] = Math.cos(endrad) * Math.sqrt(2);
    result['endY'] = -Math.sin(endrad) * Math.sqrt(2);

    return result;
  }

  /// 拨打电话
  Future<void> makePhoneCall({String phone = "18367151969"}) async {
    var url = "tel:$phone";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }


}
